import axios from "axios"
import Vue from "vue"
import router from './router/index'
// import store from './store/index'
const http = axios.create({
    baseURL: process.env.VUE_APP_API_URL || '/admin/api/'
    // baseURL: "http://localhost:3000/admin/api/"
})

//请求拦截器
http.interceptors.request.use(res=>{
    // const token = store.state.token;
    const token = sessionStorage.getItem("token")
    if(token){
        res.headers.Authorization = 'Bearer ' + token
    }
    return res
},err=>{
    return err
})

//响应拦截器
http.interceptors.response.use(res=>{
    return res
},err=>{
    console.log(err, '')
    if(err.response.data.message=="jwt expired"){
        router.push('/Login')
        Vue.prototype.$message({
            type: "warning",
            message: "登陆超时，请重新登陆"
        })
    } else if(err.response.data.message){
        Vue.prototype.$message({
            type: "warning",
            message: err.response.data.message
        })
    }
    return Promise.reject(err);
})
export default http